package org.brazilo.movi.miaamiko;

import java.io.IOException;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	ListView listView;
	 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        listView = (ListView) findViewById(R.id.list);
 
        List<Indice> indices = null;
        try {
            XMLPullParserHandler parser = new XMLPullParserHandler();
            indices = parser.parseIndice(getAssets().open("licoes.xml"));
            ArrayAdapter<Indice> adapter =
                new ArrayAdapter<Indice>(this,R.layout.list_item, indices);
            listView.setAdapter(adapter);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        listView.setClickable(true);
        listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
				Object o = listView.getItemAtPosition(position);
				Indice str=(Indice)o;
				Intent intent = new Intent(getBaseContext(), LicaoActivity.class);
				intent.putExtra("LICAOXML", String.valueOf(str.getId()));
				startActivity(intent);	
				finish();
			}
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
	public boolean onMenuItemSelected(int featuredId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sair:
			 new StandardDialog(this, uiMsgHandler, "Sair",
			            "Deseja Sair?", StandardDialog.dlgTypeYesNo, 0);
			return true;
		case R.id.sobre:
			new StandardDialog(this, uiMsgHandler, "",
		            "", StandardDialog.dlgTypeAbout, 0);
			return true;
		}
		return false;
	}

	@SuppressLint("HandlerLeak")
	private Handler uiMsgHandler = new Handler() {
	    @Override
	    public void handleMessage(Message msg) {
	        if (msg != null) {
	            if (msg.what == (StandardDialog.dlgResultYes)) {
	            	finish();
	            }
	        }
	    }
	};
}
